Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
I am trying to assign value for index[arr[i][j]]['x'], i am getting the Cannot set property 'x' of undefined error

Here is my code for reference. I'm getting error for line 9 and 10. Is there a way to assign values for dictionary inside an initially empty dictionary :

function anticlockwiseRotate(arr){
            //store the index of elements in a dict
            let index={};
            for(let i=0;i<3;i++){
                for(let j=0;j<3;j++){
                    if(!index.hasOwnProperty(arr[i][j]['x']) && !index.hasOwnProperty(arr[i][j]['y'])){
                        //left shift by 2 and change rowInde)
                        //left shift by 2 and change rowIndex->columnIndex 
                        index[arr[i][j]]['x'] = (Math.abs(j-2));
                        index[arr[i][j]]['y']=i;
                    }
                }
            }
            let rotatedArr= [];
            for(let idx in index){
                rotatedArr[index[idx][x]][index[idx][y]]=idx;
            }
            return rotatedArr
        }
        console.log(anticlockwiseRotate([[1,2,3],[4,5,6],[7,8,9]]))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

set index[arr[i][j]] as object before assigning any properties to index[arr[i][j]]

index[arr[i][j]] = {};
//left shift by 2 and change rowInde)
//left shift by 2 and change rowIndex->columnIndex 
index[arr[i][j]]['x'] = (Math.abs(j-2));
index[arr[i][j]]['y']=i;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda